package com.zengdada.authority.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Util {
    //提供加密N次
    public static String encodeBase64(String str, int n) {
        int num = (n <= 0) ? 1 : n;
        String code = str;
        if (code != null && !code.equals(""))
            for (int i = 0; i < num; i++) {
                BASE64Encoder encoder = new BASE64Encoder();
                try {
                    code = encoder.encode(code.getBytes());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        return code;
    }

    //对应提供解密N次
    public static String decodeBase64(String code, int n) {
        int num = (n <= 0) ? 1 : n;
        String str = code;
        if (str != null && !str.equals(""))
            for (int i = 0; i < num; i++) {
                BASE64Decoder decoder = new BASE64Decoder();
                try {
                    byte[] by = decoder.decodeBuffer(str);
                    str = new String(by);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        return str;
    }

    ///////////////////////////////////////////////////////////////////
    public static String encodeBase64(String str) {
        return encodeBase64(str, 1);
    }

    public static String decodeBase64(String code) {
        return decodeBase64(code, 1);
    }
}
